package com.caoji.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caoji.advice.lyExcption;
import com.caoji.constants.MQConstants;
import com.caoji.redis.Redisutil;
import com.caoji.user.UserDto;
import com.caoji.user.config.PasswordConfig;
import com.caoji.user.entity.User;
import com.caoji.user.mapper.UserMapper;
import com.caoji.user.service.UserService;

import com.caoji.utils.RegexUtils;
import com.sun.xml.internal.fastinfoset.util.PrefixArray;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private static final String KEY_PRX ="user.rigster" ;
    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    BCryptPasswordEncoder passwordEncoder;

    @Autowired
    AmqpTemplate amqpTemplate;

    @Override
    public UserDto Login(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = baseMapper.selectOne(wrapper);
        if (user==null){
            throw new lyExcption(400, "用户不存在");
        }
        if (!passwordEncoder.matches(password,user.getPassword())) {
            throw new lyExcption(400, "密码错误");
        }
        return new UserDto(user);
    }

    @Override
    public void resisty(User user, String code) {
        //判断code

        String key=KEY_PRX+user.getPhone();
        if (StringUtils.isEmpty(code)||!code.equals(redisTemplate.opsForValue().get(key))){
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            boolean save = save(user);
            System.out.println(save);
            if (!save){
                throw new lyExcption(400, "注册失败");
            }
        }

    }

    //1是用户名 2是手机号
    @Override
    public Boolean isexist(String data, Integer type) {
        if(type != 1 && type != 2){
            throw new lyExcption(400, "请求参数有误");
        }
        // 校验手机或用户名是否存在
        return query()
                .eq(type == 1, "username", data)
                .eq(type == 2, "phone", data)
                .count() == 1;
    }

    @Override
    public void sensms(String phone) {

        if (!RegexUtils.isPhone(phone)) {
            throw new lyExcption(400, "请输入正确的手机格式");
        }
        String code = RandomStringUtils.randomNumeric(6);

        redisTemplate.opsForValue().set(KEY_PRX +phone, code,5, TimeUnit.MINUTES);

        Map<String, String> msg = new HashMap<>();
        msg.put("phone", phone);
        msg.put("code", code);
        amqpTemplate.convertAndSend(MQConstants.ExchangeConstants.SMS_EXCHANGE_NAME, MQConstants.RoutingKeyConstants.VERIFY_CODE_KEY, msg);
    }
}